﻿using AutoMapper;
using E_Commerce.Domain.Dto;
using E_Commerce.Domain.Entities;
using E_Commerce.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace E_Commerce.Application
{
    /// <summary>
    /// 秒杀模块
    /// </summary>
   public class SeckillService:ISeckillService
    {
        //依赖注入
        private ISeckillRepository _repository;
        private IMapper _mapper;    //依赖注入autoMapper
        public SeckillService(ISeckillRepository repository, IMapper mapper)
        {
            _repository = repository;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加秒杀商品到订单表
        /// </summary>
        /// <param name="orders"></param>
        /// <returns></returns>
        public async Task<int> AddOrderAsync(OrdersDto orders)
        {
            try
            {
                Orders ord = _mapper.Map<Orders>(orders);
                return await _repository.AddOrderAsync(ord);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }
        /// <summary>
        /// 添加秒杀商品
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public async Task<int> AddSeckillAsync(SeckillDto s)
        {
            try
            {
                Seckill seckill = _mapper.Map<Seckill>(s);
                return await _repository.AddSeckillAsync(seckill);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }
        /// <summary>
        /// 秒杀商品详情
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public SeckillDto FillSeckill(int productId)
        {
            SeckillDto seckill =  _mapper.Map<SeckillDto>(_repository.FillSeckill(productId));
            return seckill;
        }

        /// <summary>
        /// 秒杀商品显示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<SeckillDto> GetSeckillDtos(int pageIndex, int pageSize, out int totalCount)
        {
            return _repository.GetSeckill(pageIndex,pageSize,out totalCount);
        }
        /// <summary>
        /// 修改秒杀商品状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> UptSeckillAsync(int id)
        {
            return await _repository.UptSeckillAsync(id);
        }
    }
}
